package com.lyf.viewapp.list.one;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.lyf.viewapp.R;
import com.lyf.viewapp.util.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 多选RecyclerView
 *
 *
 * @author lyf
 */
public class MultiSelectActivity extends AppCompatActivity {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.count)
    TextView count;
    @BindView(R.id.esc)
    Button esc;
    @BindView(R.id.all)
    Button all;

    /**
     * 是否处于多选状态
     */
    private boolean isMultiSelect;
    /**
     * 原始数据
     */
    private List<SelectBean> data;
    /**
     * 选中数据
     */
    private List<SelectBean> dataSelect;
    private MultiSelectAdapter adapter;

    /**
     * 全选反选状态判断
     */
    private boolean isAllSelect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_select);
        ButterKnife.bind(this);

        data = new ArrayList<>();
        dataSelect = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            data.add(new SelectBean("MultiSelectActivity_Item" + i));
        }

        adapter = new MultiSelectAdapter(this, data, isMultiSelect, new MultiSelectAdapter.OtherOnClick() {
            @Override
            public void onLongClick(int position) {
                // 长按之后的监听 改变数据等
                isMultiSelect = true;
                dataSelect.add(data.get(position));
                count.setVisibility(View.VISIBLE);
                esc.setVisibility(View.VISIBLE);
                all.setVisibility(View.VISIBLE);
                setCount();
            }

            @Override
            public void addOnClickListener(RecyclerHolder holder, int position, HashMap<Integer, Boolean> isChecked) {
                // 普通点击监听 改变数据等
                if (isMultiSelect) {
                    // 长按情况下点击
                    adapter.setIsCheckedByPosition(position);
                    if (isChecked.get(position)) {
                        dataSelect.add(data.get(position));
                    } else {
                        // 判断反选，移除选择的数据（目前由于实体类只有一个String的Name，如果有ID可根据ID来判断）
                        for (int i = 0; i < dataSelect.size(); i++) {
                            if (dataSelect.get(i).getName().equals(data.get(position).getName())) {
                                dataSelect.remove(i);
                                break;
                            }
                        }
                    }
                    setCount();
                } else {
                    LogUtils.showToast(MultiSelectActivity.this, data.get(position).getName());
                }
            }

            @Override
            public void showData(RecyclerHolder holder, int position) {
                // 初始化Item数据
                holder.tvName.setText(null != data.get(position).getName() ? data.get(position).getName() : "");
            }

            @Override
            public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(MultiSelectActivity.this)
                        .inflate(R.layout.multi_select_item, parent, false);
                return new RecyclerHolder(view);
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

    }

    /**
     * 退出
     */
    @OnClick(R.id.esc)
    public void esc() {
        dataSelect.clear();
        isMultiSelect = false;
        setCount();
        count.setVisibility(View.GONE);
        esc.setVisibility(View.GONE);
        all.setVisibility(View.GONE);
        adapter.esc();
    }

    /**
     * 全选|反选
     */
    @OnClick(R.id.all)
    public void all() {
        if (isMultiSelect) {
            dataSelect.clear();
            if (isAllSelect) {
                dataSelect.addAll(data);
            }
            adapter.setAllChecked(isAllSelect);
            isAllSelect = !isAllSelect;
            setCount();
        }
    }

    public void setCount() {
        count.setText(String.valueOf("共计:" + dataSelect.size()));
    }
}
